package cn.com.oceansoft.business.danger.controller;

import cn.com.oceansoft.business.base.controller.BaseController;
import cn.com.oceansoft.business.base.entity.Query;
import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.danger.entity.CollecterInfo;
import cn.com.oceansoft.business.danger.entity.DangerContent;
import cn.com.oceansoft.business.danger.entity.Flowinfo;
import cn.com.oceansoft.business.danger.service.ICollecterInfoService;
import cn.com.oceansoft.business.danger.service.IDangerContentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;

/**
 * Created by stt on 2018/5/17.
 */
@Controller
@RequestMapping("dangerInfo")
public class DangerInfoController extends BaseController {
    @Resource
    private ICollecterInfoService collecterInfoService;
    @Resource
    private IDangerContentService dangerContentService;
    /**
     * 隐患信息确认 街道层 拉取opertype为6的数据，确认为7 退回为8
     */
    @RequestMapping("sureJD")
    public String sureList(Model model, Query query, HttpServletRequest request,CollecterInfo collecterInfo){
        CollecterInfo cInfo =(CollecterInfo)request.getSession().getAttribute("aqyhuser");
        int mark=0;
        if("".equals(collecterInfo.getFjid())||null==collecterInfo.getFjid()){
            collecterInfo.setFjid(getMemberSession().getFjid());
            // collecterInfo=getMemberSession();
            mark=1;
            collecterInfo.setPcsid("");
            collecterInfo.setSqid("");
        }
        if("1".equals(cInfo.getRoletype())){
            collecterInfo.setPcsid(cInfo.getPcsid());
        }
        query.setData(collecterInfo);
        model.addAttribute("query",query);
        model.addAttribute("pager",dangerContentService.getNrListWithName(query));
        if("".equals(collecterInfo.getFjid())||null==collecterInfo.getFjid()){
            model.addAttribute("dicone",collecterInfoService.getdict(collecterInfo));
        }else{
            CollecterInfo one=new CollecterInfo();
            one.setFjid(collecterInfo.getFjid());
            if("".equals(getMemberSession().getFjid())||null==getMemberSession().getFjid()){
                CollecterInfo collecterInfo1=new CollecterInfo();
                collecterInfo1.setFjid("");
                model.addAttribute("dicone",collecterInfoService.getdict(collecterInfo1));
            }else{
                model.addAttribute("dicone",collecterInfoService.getonedict(one));
            }
            model.addAttribute("dictwo",collecterInfoService.getdict(one));
            if(!"".equals(collecterInfo.getPcsid())&&null!=collecterInfo.getPcsid()){
                one.setPcsid(collecterInfo.getPcsid());
                model.addAttribute("dicthree",collecterInfoService.getdict(collecterInfo));
            }

        }
        if (!"".equals(collecterInfo.getChildtype()) && null != collecterInfo.getChildtype()) {
            DangerContent dc = new DangerContent();
            dc.setType(collecterInfo.getYhlb());
            model.addAttribute("dicfour", collecterInfoService.getChildtype(dc));
        }
        return "danger/sureJD";
    }

    @ResponseBody
    @RequestMapping("save")
    public Result submit(Flowinfo flowinfo,HttpServletRequest request){
        String[] s = flowinfo.getId().split(",");
        flowinfo.setId(s[s.length-1]);
        flowinfo.setIschange(flowinfo.getIschange().split(",")[flowinfo.getIschange().split(",").length-1]);
        flowinfo.setIspenalty(flowinfo.getIspenalty().split(",")[flowinfo.getIspenalty().split(",").length-1]);
        CollecterInfo collecterInfo =(CollecterInfo)request.getSession().getAttribute("aqyhuser");

        if("8".equals(flowinfo.getOpertype())){
            dangerContentService.updateCfZg(flowinfo.getId(),flowinfo.getDangerid());
        }
        flowinfo.setId(UUID.randomUUID().toString());
        flowinfo.setCollecterid(collecterInfo.getLoginname());
        flowinfo.setCreatetime(new Date());

        return dangerContentService.submitFlowInfo(flowinfo);
    }

    @ResponseBody
    @RequestMapping("sh")
    public Result sh(HttpServletRequest request){
        String s = request.getParameter("type");
        String type = "6";
        if("1".equals(s)){
            type = "7";
        }
        Flowinfo flowinfo = dangerContentService.getFlowInfo(request.getParameter("id"),type);
        CollecterInfo collecterInfo =(CollecterInfo)request.getSession().getAttribute("aqyhuser");
        flowinfo.setCollecterid(collecterInfo.getLoginname());
        flowinfo.setCreatetime(new Date());
        if("1".equals(s)){
            flowinfo.setOpertype("5");
        }else {
            flowinfo.setOpertype("7");
        }

        return dangerContentService.submitFlowInfo(flowinfo);
    }

    @ResponseBody
    @RequestMapping("th")
    public Result th(HttpServletRequest request,Flowinfo flow){
        String s = request.getParameter("type");
        String type = "6";
        if("1".equals(s)){
            type = "7";
        }
        Flowinfo flowinfo = dangerContentService.getFlowInfo(request.getParameter("id"),type);
        flowinfo.setBackreason(flow.getBackreason());
        dangerContentService.updateCfZg(flowinfo.getId(),flowinfo.getDangerid());
        CollecterInfo collecterInfo =(CollecterInfo)request.getSession().getAttribute("aqyhuser");
        flowinfo.setId(UUID.randomUUID().toString());
        flowinfo.setCollecterid(collecterInfo.getLoginname());
        flowinfo.setCreatetime(new Date());
        flowinfo.setOpertype("8");
        return dangerContentService.submitFlowInfo(flowinfo);
    }


    @RequestMapping("th/page")
    public String thPage(HttpServletRequest request,Model model){
        String id = request.getParameter("id");
        String type = request.getParameter("type");
        model.addAttribute("id",id);
        if("1".equals(type)){
            return "danger/thQ";
        }
        return "danger/thJD";
    }


    @RequestMapping("sureQ")
    public String sureListQ(Model model, Query query, HttpServletRequest request,CollecterInfo collecterInfo){
        CollecterInfo cInfo =(CollecterInfo)request.getSession().getAttribute("aqyhuser");
        int mark=0;
        if("".equals(collecterInfo.getFjid())||null==collecterInfo.getFjid()){
            collecterInfo.setFjid(getMemberSession().getFjid());
            // collecterInfo=getMemberSession();
            mark=1;
            collecterInfo.setPcsid("");
            collecterInfo.setSqid("");
        }
        if("0".equals(cInfo.getRoletype())){
            if(cInfo.getFjid()!=null){
                collecterInfo.setFjid(cInfo.getFjid());
            }
        }
        query.setData(collecterInfo);
        model.addAttribute("query",query);
        model.addAttribute("pager",dangerContentService.getNrListWithNameQ(query));
        if("".equals(collecterInfo.getFjid())||null==collecterInfo.getFjid()){
            model.addAttribute("dicone",collecterInfoService.getdict(collecterInfo));
        }else{
            CollecterInfo one=new CollecterInfo();
            one.setFjid(collecterInfo.getFjid());
            if("".equals(getMemberSession().getFjid())||null==getMemberSession().getFjid()){
                CollecterInfo collecterInfo1=new CollecterInfo();
                collecterInfo1.setFjid("");
                model.addAttribute("dicone",collecterInfoService.getdict(collecterInfo1));
            }else{
                model.addAttribute("dicone",collecterInfoService.getonedict(one));
            }
            model.addAttribute("dictwo",collecterInfoService.getdict(one));
            if(!"".equals(collecterInfo.getPcsid())&&null!=collecterInfo.getPcsid()){
                one.setPcsid(collecterInfo.getPcsid());
                model.addAttribute("dicthree",collecterInfoService.getdict(collecterInfo));
            }

        }
        if (!"".equals(collecterInfo.getChildtype()) && null != collecterInfo.getChildtype()) {
            DangerContent dc = new DangerContent();
            dc.setType(collecterInfo.getYhlb());
            model.addAttribute("dicfour", collecterInfoService.getChildtype(dc));
        }
        return "danger/sureQ";
    }
}
